package com.快速排序;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Stack;

public class Test {

	public static void main(String[] args) {
		// int a[] = {4,3,2,1,5,7,8,4,2,1};
		// Solution.quicksort2(a, 0, a.length-1);
		// System.out.println(Arrays.toString(a));
		// List<Integer> list =new ArrayList<Integer>();
		// Stack s = new Stack();
		// s.e
		// Double i=1;
		//// add(i);
		// System.out.println(i);
		// short s = 1;s+=1;
		// System.out.println(Math.round(-11));

		String s = "PTB abcd ab";
	    Test t = new Test();
	    String re = t.reverseSentence(s, s.length());
	    System.out.println(re);

	}

	public String reverseSentence(String A, int n) {
		// 第一步将整个字符串都逆置
		char a[] = A.toCharArray();
		reserve(0, n - 1, a);
		
		int start = 0, end = 0;
		int i,j;
		for ( i = 0, j = 0; i < n; i++) {
			
			if (a[i] == ' ') {
				start = j;
				end = i - 1;
				
				reserve(start, end, a);
				
				j = i + 1;
			}
		}
		reserve(j,n-1,a);
		
		String result = new String(a);
		return result;
	}

	public void reserve(int start, int end, char[] a) {
		while(start<end) {
			char temp = a[start];
			a[start] = a[end];
			a[end] = temp;
			start++;
			end--;
		}

	}
}


